package com.fowo.api.service.handleWork;

import com.fowo.api.model.process.product.ProcessProductVo;
import com.fowo.api.model.process.product.item.ProcessProductItemItemVo;
import com.fowo.api.model.stock.in.StockInPushprocessAction;
import com.fowo.api.model.stock.in.StockInVo;
import com.fowo.api.model.stock.in.product.StockInProductItemVo;
import com.fowo.api.service.ProcessProductItemService;
import com.fowo.api.service.ProcessProductService;
import com.fowo.api.service.StockInProductService;
import com.fowo.api.service.StockInService;
import com.fowo.api.service.impl.ProcessProductItemServiceImpl;
import com.fowo.api.service.impl.ProcessProductServiceImpl;
import com.fowo.api.service.impl.StockInServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class StockInPushprocessActionImpl implements StockInPushprocessAction {
    @Resource
    protected StockInServiceImpl stockInService;

    @Resource
    protected StockInProductService stockInProductService;

    @Resource
    protected ProcessProductServiceImpl processProductService;

    @Override
    public void run(List<Long> idList) throws Exception {
            for(int i=0;i<idList.size();i++){
                StockInVo voById = stockInService.getVoById(idList.get(i));
                hanlde(voById);
            }
    }

    private void hanlde(StockInVo voById) throws Exception {
        ProcessProductVo model=new ProcessProductVo();
        List<ProcessProductItemItemVo> processProductItems=new ArrayList<>();

        for(int i=0;i<voById.getProductList().size();i++){
            StockInProductItemVo stockInProductItemVo = voById.getProductList().get(i);
            ProcessProductItemItemVo ProcessProductItemItemVo=new ProcessProductItemItemVo();
            ProcessProductItemItemVo.setProductId(stockInProductItemVo.getProductId());
            ProcessProductItemItemVo.setSku(stockInProductItemVo.getSku());
            ProcessProductItemItemVo.setNum(Long.valueOf(stockInProductItemVo.getQuantity()));
            processProductItems.add(ProcessProductItemItemVo);
        }
        model.setProcessProductItems(processProductItems);
        processProductService.create(model);
    }
}
